#ifndef WL_EXT_HTTP_H
#define WL_EXT_HTTP_H

typedef void* wl_ext_http_handle;

//type 0: get file, 1: get data, 2: post
enum {
    WL_EXT_HTTP_GET_FILE = 0,
    WL_EXT_HTTP_GET_DATA,
    WL_EXT_HTTP_POST
};

enum {
    WL_EXT_HTTP_HEADER_STATUS,
    WL_EXT_HTTP_BODY_DATA_STATUS,
    WL_EXT_HTTP_PROGRESS_STATUS,
    WL_EXT_HTTP_FINISH_STATUS,
    WL_EXT_HTTP_ERROR_STATUS
};

/**
 * @brief http callback
 * 
 * @param handle http handle
 * @param status http status: header, body, finish, error
 * @param pData data 如果是获取文件，返回的是文件名
 * @param dataLen data length
 */
typedef void (*wl_ext_http_callback)(wl_ext_http_handle handle, int status, void* pData, unsigned int dataLen);


/**
 * @brief http的初始化，初始化平台相关性的东西
 * 
 * @return 0 success,  < 0 fail
 * 
 */
int wl_ext_http_init(void);

/**
 * @brief deinit http
 * 
 */
void wl_ext_http_deinit(void);

/**
 * @brief 创建http的一个句柄，后续所有操作围绕此句柄展开。有了句柄，说明后续的每一次request的http申请都是独立的，
 *         也就是说可以同时进行多个http请求
 * 
 * @param url url
 * @param type 类型依据上面的枚举，有三项，0: get file, 1: get data, 2: post
 * @return http handle，如果返回null，说明创建失败
 *  
 */
wl_ext_http_handle wl_ext_http_request_create(const char *url, int type);

/**
 * @brief 这是获取http get的数据，数据内容通过回调返回
 * 
 * @param handle http handle
 * @param callback callback
 * @param pContext user context
 *  
 * @return 0 success, < 0 fail
 */
int wl_ext_http_get_data(wl_ext_http_handle handle, wl_ext_http_callback callback, void *pContext);


/**
 * @brief 这是获取http get的申请，但数据会预先存储在用户传入的指定路径，目前是传入相对路径，实现者要依据平台自己填充完整路径
 *        如果传入的路径为空，那么实现者要求会自动生成一个文件名，并在回调中返回
 * 
 * @param handle http handle
 * @param filePath file path, if file is null, should generate a file name
 * @param callback callback
 * @param pContext user context
 *  
 * @return 0 success, < 0 fail
 */
int wl_ext_http_get_file(wl_ext_http_handle handle, const char *filePath, wl_ext_http_callback callback, void *pContext);


/**
 * @brief http post data. the result data will be supplied in callback
 * 
 * @param handle http handle
 * @param pBody pBody data, post数据，要json格式的字符串
 * @param bodyLen body length
 * @param callback callback
 * @param pContext user context
 *  
 * @return 0 success, < 0 fail
 */
int wl_ext_http_post_data(wl_ext_http_handle handle, const char *pBody, unsigned int bodyLen, wl_ext_http_callback callback, void *pContext);

/**
 * @brief http set header
 * 
 * @param handle http handle
 * @param key header key  头部的key名
 * @param value header value 头部的value值
 * 
 * @return 0 success, < 0 fail
 *  
 */
int wl_ext_http_set_header(wl_ext_http_handle handle, const char *key, const char *value);

/**
 * @brief http post file. the result data will be supplied in callback
 * 
 * @param handle http handle
 *
 */
void wl_ext_http_close(wl_ext_http_handle handle);

/**
 * @brief http get context
 * 
 * @param handle http handle
 * @return user context
 *  
 */
void* wl_ext_http_get_context(wl_ext_http_handle handle);

/**
 * @brief 返回当前handle对应的申请http请求的类型，是0: get file, 1: get data, 2: post
 * 
 * @param handle http handle
 * @return 0: get file, 1: get data, 2: post
 *  
 */
int wl_ext_http_get_type(wl_ext_http_handle handle);
#endif
